package org.steven.crm.app.api.processor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.steven.validate.DateValidator;
import com.steven.validate.IntegerValidator;
import com.steven.validate.PriceValidator;
import com.steven.validate.StringValidator;
import com.steven.xweb.web.CheckList;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.common.usermodel.LineStyle;
import org.steven.crm.app.api.processor.base.BaseCURDProcessor;
import org.steven.crm.core.Constants;
import org.steven.crm.core.dto.salebill.SaveSaleBillProductRequest;
import org.steven.crm.core.dto.salebill.SaveSaleBillRequest;
import org.steven.crm.core.service.BaseService;
import org.steven.crm.core.service.product.ProductService;
import org.steven.crm.core.service.salebill.SaleBillService;
import org.steven.crm.core.utils.JSONUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author wuzw
 * @Date 2024/6/27 0:19
 * @Version 1.0
 */
public class SaleBillProcessor extends BaseCURDProcessor {
    private static SaleBillService service = new SaleBillService();
    @Override
    protected BaseService getService() {
        return service;
    }

    public boolean saveActionCheck() throws Exception {
        CheckList checkList = getChecklist();
        checkList.addCheckItem(new StringValidator("status", "销售单状态", getStringData("status"), true));
        checkList.addCheckItem(new DateValidator("billDate", "销售日期", getStringData("billDate"), true));
        checkList.addCheckItem(new StringValidator("customerId", "客户", getStringData("customerId"), true));
        checkList.addCheckItem(new PriceValidator("freightAmount", "运费", getStringData("freightAmount"), true));
        checkList.addCheckItem(new PriceValidator("discountAmount", "优惠金额", getStringData("discountAmount"), true));
        checkList.addCheckItem(new StringValidator("payStatus", "付款状态", getStringData("payStatus"), true));
        if (getStringData("payStatus").equals(Constants.PayStatus.PAY_SOME)) {
            checkList.addCheckItem(new PriceValidator("payAmount", "已付款金额", getStringData("payAmount"), true));
        }
        return checkList.check();
    }

    public void saveAction() throws Exception {
        Map<String, String> data = getStringDatas();
        List<Map<String, String>> products = JSON.parseObject(data.remove("products"), new TypeReference<List<Map<String, String>>>() {});
        List<String> images = JSON.parseObject(data.remove("images"), new TypeReference<List<String>>() {});

        SaveSaleBillRequest saveSaleBillRequest = JSONUtil.toBean(data, SaveSaleBillRequest.class);
        saveSaleBillRequest.setImages(images);

        List<SaveSaleBillProductRequest> productsObj = new ArrayList<>();
        for (Map<String, String> product : products) {
            SaveSaleBillProductRequest saveSaleBillProductRequest = JSONUtil.toBean(product, SaveSaleBillProductRequest.class);
            productsObj.add(saveSaleBillProductRequest);
        }
        saveSaleBillRequest.setProducts(productsObj);
        for (SaveSaleBillProductRequest product : productsObj) {
            if (StringUtils.isEmpty(product.getProductId())) {
                throw new Exception("产品id不能为空");
            }

            if (!PriceValidator.check(product.getPrice())) {
                throw new Exception("产品单价不正确");
            }

            if (!PriceValidator.check(product.getNum())) {
                throw new Exception("产品数量不正确");
            }
        }
        service.save(saveSaleBillRequest);
    }
}
